package cn.crism.service;

import cn.crism.dao.SysDeptMapper;
import cn.crism.dto.DeptLevelDto;
import cn.crism.model.SysDept;
import cn.crism.utils.LevelUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @CreateDate: 2018/12/13 6:00 PM
 * @Description:
 * @Author: Crism
 */

@Service
public class SysTreeService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    public List<DeptLevelDto> deptTree() {
        /* 查询数据库获取所有部门信息 */
        List<SysDept> deptList = sysDeptMapper.getAllDept();
        List<DeptLevelDto> dtoList = Lists.newArrayList();
        for (SysDept dept : deptList) {
            DeptLevelDto dto = DeptLevelDto.adapt(dept);
            dtoList.add(dto);
        }
        return deptListToTree(dtoList);
    }

    public List<DeptLevelDto> deptListToTree(List<DeptLevelDto> deptLevelList) {
        /* 没有数据返回空 */
        if (CollectionUtils.isEmpty(deptLevelList)) {
            return Lists.newArrayList();
        }
        /* level -> [dept1,dept2]  类似 Map<String,<List>> */
        Multimap<String, DeptLevelDto> levelDtoMap = ArrayListMultimap.create();

        List<DeptLevelDto> rootList = Lists.newArrayList();
        for (DeptLevelDto dto : deptLevelList) {
            /* level ：dto  */
            levelDtoMap.put(dto.getLevel(), dto);
            /* 顺便取出所有顶层元素 */
            if (LevelUtil.ROOT.equals(dto.getLevel())) {
                rootList.add(dto);
            }
        }
        /* 按照seq从小到大依次排序 */
        Collections.sort(rootList, deptSeqComparator);
        /* 递归生成树 */
        transformDeptTree(rootList, LevelUtil.ROOT, levelDtoMap);
        return rootList;
    }


    public void transformDeptTree(List<DeptLevelDto> deptLevelList, String level, Multimap<String, DeptLevelDto> levelDtoMap) {

        /* 遍历该层元素 */
        for (int i = 0; i < deptLevelList.size(); i++) {
            DeptLevelDto deptLevelDto = deptLevelList.get(i);
            /* 拼接得到下层level */
            String nextLevel = LevelUtil.calculateLevel(level,deptLevelDto.getId());
            /* 根据nextLevel获取下层所有元素 */
            List<DeptLevelDto> tempDeptList =(List<DeptLevelDto>) levelDtoMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempDeptList)){
                /* 对下层元素排序 */
                Collections.sort(tempDeptList,deptSeqComparator);
                /* 设置下层列表元素 */
                deptLevelDto.setDeptList(tempDeptList);
                /* 设置下层元素的子素 */
                transformDeptTree(tempDeptList,nextLevel,levelDtoMap);
            }
        }
    }

    /* 排序 */
    public Comparator<DeptLevelDto> deptSeqComparator = new Comparator<DeptLevelDto>() {
        @Override
        public int compare(DeptLevelDto o1, DeptLevelDto o2) {
            return o1.getSeq() - o2.getSeq();
        }
    };
}
